<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue-shopping</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
          crossorigin="anonymous">
    <style>
        [v-cloak] {
            visibility: hidden;
        }
    </style>
</head>
<body>

<div id="app" v-cloak>
    <table class="table table-bordered">
        <thead>
        <tr>
            <th>#</th><th>商品名称</th><th>商品单价</th><th>购买数量</th><th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(item, index) in list">
            <th scope="row">{{ index+1 }}</th>
            <td>{{ item.name }}</td>
            <td>{{ item.price }}</td>
            <td>
                <button type="button" class="btn btn-default"
                        @click="addNum(index)">+</button>
                {{ item.number }}
                <button type="button" class="btn btn-default"
                        @click="reduceNum(index)" :disabled="item.number === 1">-</button>
            </td>
            <td>
                <button type="button" class="btn btn-default"
                    @click="changeIsBuy(index)">{{ item.isBuy?"取消":"选中" }}</button>
            </td>
        </tr>
        </tbody>
    </table>
    <div v-if="list.length">
        <button type="button" class="btn btn-default"
            @click="selectAll()">全选</button>
        <p>总价：{{ totalPrice }}</p>
    </div>
    <div v-else>
        购物车为空
    </div>
</div>

<script src="https://unpkg.com/vue"></script>
<script>
    let app = new Vue({
        el: '#app',
        data: {
            list: [
                {
                    name: 'iPhone 7',
                    price: 6188,
                    number : 1,
                    isBuy: true
                }, {
                    name: 'iPad Pro',
                    price: 5888,
                    number : 1,
                    isBuy: true
                },{
                    name: 'MacBook Pro',
                    price: 21488,
                    number : 1,
                    isBuy: true
                }
            ]
        },
        computed: {
            totalPrice() {
                let totalPrice = 0;
                this.list.forEach((item) => {
                    if (item.isBuy === true)
                        totalPrice += (item.price * item.number);
                });
                return totalPrice;
            }
        },
        methods: {
            addNum(index) {
                this.list[index].number++;
            },
            reduceNum(index) {
                if (this.list[index].count === 1) return;
                this.list[index].number--;
            },
            removeItem(index) {
                this.list.splice(index, 1);
            },
            changeIsBuy(index) {
                this.list[index].isBuy = !this.list[index].isBuy;
            },
            selectAll() {
                this.list.forEach((item)=> {
                    item.isBuy = true;
                })
            }
        }
    })

</script>
</body>
</html>